package org.springcloud.wisdom.springcloud_03_freshman.admin.service;

import org.springcloud.wisdom.springcloud_03_freshman.admin.dao.AdminDormDao;
import org.springcloud.wisdom.springcloud_03_freshman.common.bean.Page;
import org.springcloud.wisdom.springcloud_03_freshman.student.entity.Card;
import org.springcloud.wisdom.springcloud_03_freshman.student.entity.Dormitory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminDormService {
    @Autowired
    AdminDormDao adminDormDao;
    public Page<Dormitory> getDormPage(int pageNum, int pageSize){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Dormitory> list=adminDormDao.getAll(start,pageSize);
        //获取总条目数
        int total=adminDormDao.countCards();
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Dormitory> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    public  boolean add(Dormitory dormitory){
        if (dormitory==null){
            return false;
        }
        dormitory.setOccupy("未满员");
        return adminDormDao.addDorm(dormitory)>0;
    }

    public  boolean update(Dormitory dormitory){
        if (dormitory==null){
            return false;
        }
        return adminDormDao.updateDorm(dormitory)>0;
    }

}
